﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using Microsoft.Phone.Tasks;

namespace WeatherFrame
{
    public partial class LocationListView : UserControl, IDataContextChangedHandler<LocationListView>
    {
        public LocationListView()
        { 
            InitializeComponent();
            Tag = Visibility.Collapsed;
            DataContextChangedHelper<LocationListView>.Bind(this);
        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (App.ViewModel.LocationState == LocationState.Searching) return;                
            if (e.Key == Key.Enter)
            {
                var tb = (TextBox)sender;
                string s = tb.Text;
                if (s != null) s = s.Trim();
                if (!string.IsNullOrEmpty(s))
                {
                    App.ViewModel.SearchLocation(s);
                    NothingFound.Text = "No locations found for '" + s + "'.";
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (App.ViewModel.LocationState != LocationState.Normal) return;
            App.ViewModel.LocationState = LocationState.Editing;
            Button b = (Button)sender;
            var gr = b.Parent as Grid;
            var tb = gr.FindName("LocationTextBox") as TextBox;
            tb.Text = "";
            tb.Focus();
        }

        private void ToggleButton_Checked(object sender, RoutedEventArgs e)
        {
            var tb = sender as ToggleButton;
            UpdateToggle(tb);
        }

        private void UpdateToggle(ToggleButton tb)
        {
            var EditIcon = tb.FindName("EditIcon") as Image;
            var EditDone = tb.FindName("EditDone") as TextBlock;
            EditIcon.Visibility = tb.IsChecked == false ? Visibility.Visible : Visibility.Collapsed;
            EditDone.Visibility = tb.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
            Tag = EditDone.Visibility;
        }

        private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
        {
            var tb = sender as ToggleButton;
            UpdateToggle(tb);
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            Location l = ((Button)sender).DataContext as Location;
            if (l == null) return;
            App.ViewModel.RemoveLocation(l,false);
        }

        private void WorldWeatherOnline_Click(object sender, RoutedEventArgs e)
        {
            WebBrowserTask wbt = new WebBrowserTask{ URL = "http://www.worldweatheronline.com"};
            wbt.Show();
        }

        private void LocationTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var tb = (TextBox)sender;
            var gr = (Grid)tb.Parent;
            var tt = (TextBlock)gr.FindName("LocationTextBoxHint");
            tt.Visibility = string.IsNullOrEmpty(tb.Text) ? Visibility.Visible : Visibility.Collapsed;
        }

        private void Info_Click(object sender, RoutedEventArgs e)
        {
            App.ViewModel.ShowInfo = true;
        }       
        
        private void WeatherIcon_Loaded(object sender, RoutedEventArgs e)
        {
            Control im = (Control)sender;
            var sb = new Storyboard();
            DoubleAnimation da = new DoubleAnimation { 
                From = 0, To = 360, 
                Duration = new Duration(TimeSpan.FromSeconds(25)), 
                RepeatBehavior = RepeatBehavior.Forever 
            };
            Storyboard.SetTarget(da, im.RenderTransform);
            Storyboard.SetTargetProperty(da, new PropertyPath("Angle"));
            sb.Children.Add(da);
            sb.Begin();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            if (App.ViewModel.LocationState== LocationState.Searching) return;
            App.ViewModel.LocationState = LocationState.Normal;
        }

        private void SearchResultSelected(object sender, SelectionChangedEventArgs e)
        {
            ListBox lb = (ListBox)sender;
            Location loc = lb.SelectedItem as Location;
            if (loc==null) return;
            App.ViewModel.ConfirmLocation(loc);
        }

        #region IDataContextChangedHandler<LocationListView> Members

        public void DataContextChanged(LocationListView sender, DependencyPropertyChangedEventArgs e)
        {
            WeatherModel wm = (WeatherModel)DataContext;
            wm.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(wm_PropertyChanged);
        }

        void wm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "LocationState")
            {
                if (App.ViewModel.LocationState == LocationState.SearchResults)
                {
                    if (App.ViewModel.SearchResults.Count > 0)
                    {
                        SearchResultsList.Focus();
                    }
                }
            }
        }

        #endregion

        private void Unit_Click(object sender, RoutedEventArgs e)
        {
            App.ViewModel.UseCelsius = !App.ViewModel.UseCelsius;
        }
    }
}

